<template>
	<view>
		<navbar title="添加记录" :autoBack="true"></navbar>
		<scroll-view scroll-y="true" class="scbox">
			<view class="con">
				<view class="con__box">
					<view class="con__box__item">
						<view class="con__box__item__label">
							客户
						</view>
						<view class="con__box__item__val">
							{{name}}
						</view>
					</view>
					<u--textarea :showConfirmBar='false' v-model="form.content" autoHeight placeholder="添加跟进记录，1000字以内"
						border='none' maxlength='1000' count
						:placeholderStyle='{color:"#919191",fontSzie:"24rpx",}'></u--textarea>
					<!-- <image src="/static/tool/up1.png" class="con__box__up" mode=""></image> -->
					<view class="">
						<u-upload :fileList="fileList6" @afterRead="afterRead" @delete="deletePic" name="6" multiple
							:maxCount="9" width="149rpx" height="149rpx">
							<image src="/static/tool/up1.png" mode="widthFix" style="width: 149rpx;height: 149rpx;">
							</image>
						</u-upload>
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							下次跟进时间
						</view>
						<view class="con__box__item__val" style="flex: 1;justify-content: flex-end;" @click="show=true">
							{{form.nexttime||'无'}}
							<image src="/static/homex/more2.png" class="con__box__item__val__more" mode=""></image>
						</view>
					</view>
					<u-button color="#3262FD" text='保存' :loading="loading" @click="submit"
						:customStyle='{width:"628rpx",height:"94rpx",marginTop:"64rpx",marginLeft:0,padding:0,borderRadius:"15rpx",fontSize:"32rpx"}'></u-button>
				</view>
			</view>
		</scroll-view>
		<u-datetime-picker ref="datetimePicker" :show="show" weeks='datetime' @close='show=false' :minDate='minDate'
			@cancel='show=false' @confirm='confirmtime' closeOnClickOverlay v-model="value1" mode="datetime"
			:formatter="formatter"></u-datetime-picker>
	</view>
</template>

<script>
	import sale from '@/api/_sale.js'
	export default {
		data() {
			return {
				value1: Number(new Date()),
				minDate: Number(new Date()),
				form: {
					type: "1",
					customer_id: "",
					images: "",
					nexttime: "",
					content: ''
				},
				name: '',
				fileList6:[],
				loading:false,
				show:false
			}
		},
		methods: {
			confirmtime(e){
				this.form.nexttime=uni.$u.timeFormat(e.value, 'yyyy-mm-dd hh:MM');
				this.show=false
			},
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
			
				return value
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				let _this = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "http://dn.fuliaoxx.com/api/common/upload", // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							user: "test",
						},
						success: (res) => {
							setTimeout(() => {
								resolve(JSON.parse(res.data).data);
							}, 1000);
						},
					});
				});
			},
			async submit(){
				let _this = this
				let arr = []
				this.fileList6.map(t => {
					arr.push(t.url.url)
				})
				this.form.images = arr.join(',')
				this.loading=true
				let res = await sale.createCustomerRecord(this.form)
				if(res.data.code==1){
					uni.$u.toast('保存成功')
					setTimeout(()=>{
						uni.navigateBack()
						_this.loading=false
					},1000)
					
				}else{
					this.loading=false
				}
			}
		},
		onLoad(o) {
			this.name = o.name
			this.form.customer_id = o.id
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f8f8f8;
	}

	.con {
		padding: 20rpx 30rpx;

		&__box {
			background: #FFFFFF;
			border-radius: 20rpx;
			padding-left: 30rpx;
			padding-bottom: 69rpx;

			&__item {
				height: 110rpx;
				border-bottom: 2rpx solid #F9F9F9;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding-right: 30rpx;

				&__label {
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #6D6D6D;
					line-height: 26rpx;
				}

				&__val {
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #020202;
					line-height: 26rpx;
					display: flex;
					align-items: center;

					&__more {
						width: 12rpx;
						height: 21rpx;
						margin-left: 15rpx;
					}
				}
			}

			&__up {
				width: 149rpx;
				height: 149rpx;
				margin-top: 34rpx;
			}
		}
	}

	::v-deep.u-textarea {
		padding: 28rpx 0 80rpx;

		.u-textarea__field {
			font-size: 24rpx;
		}
	}
</style>